package mytest.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import mytest.util.RequestUtil;
import mytest.util.Response;
import test.service.ServiceUser;
import test.testcase.dao.UserDao;
import test.user.user;
import test.utils.GetValue;
import test.utils.JsonResult;

@Controller @RequestMapping(value = "/user", produces = "application/json; charset=utf-8")
@CrossOrigin
public class users {
	
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public @ResponseBody String login(@RequestBody String body) {
		JsonResult result = JsonResult.parse(body);
		String userName = result.read("username").toString();
		user user = ServiceUser.getUserByUserName(userName);
		if (user == null) {
			Response response = new Response();
			response.setRetCodeFalse();
			response.setRetMessage("登录失败，用户名密码错误");
			return response.toString();
		}else {
			if (user.getPwd().equals(result.read("password").toString())) {
				int num = GetValue.randomNumber(100000, 999999);
				Response response = new Response();
				response.setRetMessage("登录成功");
				List<HashMap<Object, Object>> list = new ArrayList<>();
				HashMap<Object, Object> map = new HashMap<>();
				map.put("randuid", num);
				list.add(map);
				response.setList(list);
				UserDao.saveToken(user, num);
				return response.toString();
			}else {
				Response response = new Response();
				response.setRetCodeFalse();
				response.setRetMessage("登录失败，用户名密码错误");
				return response.toString();
			}
		}
	}
	
	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public @ResponseBody String logout(HttpServletRequest request) {
		return UserDao.deleteToken(RequestUtil.getUserName(request)).toResponse();
	}
	
	@RequestMapping(value = "/adduser", method = RequestMethod.POST)
	public @ResponseBody String adduser(@RequestBody String body) {
		JsonResult result = JsonResult.parse(body);
		String username = result.read("username").toString();
		String pwd = result.read("pwd").toString();
		result = UserDao.addUser(username, pwd);
		if (result == null) {
			Response response = new Response();
			response.setRetCodeFalse();
			response.setRetMessage("用户名重复");
			return response.toString();
		}
		return result.toResponse();
	}
	
	@RequestMapping(value = "/modifypwd", method = RequestMethod.POST)
	public @ResponseBody String adduser(@RequestBody String body, HttpServletRequest request) {
		JsonResult result = JsonResult.parse(body);
		String oldpwd = result.read("oldpwd").toString();
		String newpwd1 = result.read("newpwd1").toString();
		String newpwd2 = result.read("newpwd2").toString();
		user user = RequestUtil.getUser(request);
		if (!user.getPwd().equals(oldpwd)) {
			Response response = new Response();
			response.setRetCodeFalse();
			response.setRetMessage("旧密码错误");
			return response.toString();
		}
		if (!newpwd1.equals(newpwd2)) {
			Response response = new Response();
			response.setRetCodeFalse();
			response.setRetMessage("新密码两次输入不一致");
			return response.toString();
		}
		if (newpwd1.length() < 6 || newpwd1.length() > 18) {
			Response response = new Response();
			response.setRetCodeFalse();
			response.setRetMessage("密码长度必须为6-18位");
			return response.toString();
		}
		return UserDao.repwd(user.getUsername(), newpwd1).toResponse();
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public @ResponseBody String list() {
		return UserDao.list().toResponse();
	}
	
	@RequestMapping(value = "/project/list", method = RequestMethod.GET)
	public @ResponseBody String projectuserlist(HttpServletRequest request) {
		return UserDao.projectuserlist(RequestUtil.getProjectId(request)).toResponse();
	}
	
	@RequestMapping(value = "/repwd/{id}", method = RequestMethod.GET)
	public @ResponseBody String repwd(@PathVariable String id) {
		return UserDao.repwd(id).toResponse();
	}
	
}
